<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form label-width="0px" class="login_form" :model="form" ref="formref">
        <!-- 用户名 -->
        <el-form-item>
          <el-input
            prefix-icon="el-icon-user-solid"
            placeholder="账号"
            v-model="form.username"
          >
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item>
          <el-input
            prefix-icon="iconfont icon-mima"
            placeholder="密码"
            v-model="form.password"
            type="password"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="success" @click="submit"> 登录 </el-button>
          <el-button type="info" @click="replace">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      }
      //   rules: {
      //     name: [
      //       {
      //         required: true,
      //         message: ' 请输入名称',
      //         trigger: 'blur'
      //       },
      //       {
      //         min: 3,
      //         max: 6,
      //         message: '名字需要三到五个字符',
      //         trigger: 'blur'
      //       }
      //     ]
      //   }
    }
  },
  methods: {
    replace() {
      this.form.username = 'admin'
      this.form.password = '123456'
      this.$refs.formref.resetFields()
    },
    submit() {
      // 看element。js，message是需要全局挂载的
      // this.$message({
      //     message: "恭喜你，这是一条成功消息",
      //     type: "success",
      // });
      // async 与await连用可以将返回的promise对象变成解析后的数据
      //   this.$refs.formref.validate(async valid => {
      //     console.log(valid)
      //     if (!valid) return
      //     const reslut = await this.$http.post('/api/login', this.form)
      //     console.log(reslut.data.code)
      //     if (reslut.data.code !== 200)
      //       // return console.log("登录失败");
      //       return this.$message.error('账号或者密码错误')

      //     this.$message.success({
      //       message: '登录成功'
      //     })
      //     //1 将登录成功的token保存到客户端的sessionstorage中
      //     window.sessionStorage.setItem('token', reslut.data.data.token)

      //     // //1.1 项目中出了登录之外的其他api接口，必须知道登录之后才能访问

      //     // //1.2 token只应当在当前网站打开期间生效，所以将token保存在sessionstorage中

      //     // //2 通过编程式导航跳转到后台主页，路由地址是/home
      //     this.$router.push('/home')
      //   })
      if (this.form.username == 'admin' && this.form.password == '123456') {
        this.$message.success({
          message: '登录成功'
        })
        window.sessionStorage.setItem('token', '1234')
        this.$router.push('/home')
        console.log(123)
      } else {
        this.$message.error({
          message: '登录失败，账号或者密码错误'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  background-color: aqua;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      height: 100%;
      width: 100%;
      background-color: #eee;
      border-radius: 50%;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
  padding-right: 10px;
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
